<?php

namespace App\Game;

use Carbon\Carbon;
use App\Game\Traits\PtTrait;

class pt extends Game
{
    use PtTrait;

    private $platName = 'pt';


    const API_BASE_PEM = 'pt.pem';
    const API_BASE_KEY = 'pt.key';

    public function __construct()
    {
        $this->header = [
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Cache-Control' => 'max-age=0',
            'Connection' => 'keep-alive',
            'Keep-Alive' => 'timeout=5, max=100',
            'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
            'Accept-Language' => 'es-ES,es;q=0.8',
            'Pragma' => '',
            "X_ENTITY_KEY" => config('game.pt')['line'][0]['xentityKey']
        ];
        $this->adds = [
            'main_game_plat_code' => $this->platName,
            'status' => 1
        ];


    }

    public function ptcurl($url)
    {
        $result = $this->request('GET', $url, [], $this->header, [
                'verify' => false,
                'ssl_key' => app_path('Game/Verify/' . self::API_BASE_KEY),
                'cert' => app_path('Game/Verify/' . self::API_BASE_PEM)
            ]
        );
        return $result;
    }


    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    //获取最近更新时间
    public function lastSynchronizedGameFlowTimeStamp()
    {
        if (!$this->lastSynchronizeTime && \Storage::exists('PT_synchronizedTimestamp')) {
            $this->lastSynchronizeTime = \Storage::get('PT_synchronizedTimestamp');
        }

        if (!$this->lastSynchronizeTime && !\Storage::exists('PT_synchronizedTimestamp')) {
            $this->lastSynchronizeTime = time() - $this->start * 60;
        }
        return $this->lastSynchronizeTime;
    }

    //创建用户
    public function curlCreateMember($accountUserName, $password, $request)
    {
        $url = $this->gameUrl . 'player/create/playername/' . $accountUserName . '/adminname/' . $this->adminname . '/kioskname/' . $this->kioskname . '/password/' . $password . '/custom02/' . $this->kioskname . '/custom03/11/trackingid/' . $this->kioskname;

        $output = $this->ptcurl($url);

        $outputarr = json_decode($output, true);
        if (isset($outputarr['result']['playername'])) {
            return ['success' => true, 'data' => ['accountUserName' => $outputarr['result']['playername'], 'password' => $outputarr['result']['password']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '创建失败'];
        }
    }

    //获取余额
    public function curlGetBalance($accountUserName)
    {
        $url = $this->gameUrl . 'player/info/playername/' . $accountUserName;

        $output = $this->ptcurl($url);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['result'])) {
            return ['success' => true, 'data' => ['balance' => $outputarr['result']['BALANCE']], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '查询失败'];
        }
    }

    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid)
    {
        $url = $this->gameUrl . 'player/deposit/playername/' . $accountUserName . '/adminname/' . $this->adminname . '/amount/' . $price . '/externaltranid/' . $transferid;

        $output = $this->ptcurl($url);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['result'])) {
            return ['success' => true, 'data' => ['transferid' => $outputarr['result']['ptinternaltransactionid'], 'transfer_state' => 1], 'message' => ''];
        } else if (isset($outputarr['error'])) {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => ''];
        }
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid)
    {
        $url = $this->gameUrl . 'player/withdraw/playername/' . $accountUserName . '/adminname/' . $this->adminname . '/amount/' . $price . '/externaltranid/' . $transferid;

        $output = $this->ptcurl($url);
        $outputarr = json_decode($output, true);

        if (isset($outputarr['result'])) {
            return ['success' => true, 'data' => ['transferid' => $outputarr['result']['ptinternaltransactionid'], 'transfer_state' => 1], 'message' => ''];
        } else if (isset($outputarr['error'])) {
            return ['success' => false, 'data' => ['transfer_state' => 0], 'message' => '转帐失败'];
        } else {
            return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => ''];
        }
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        return ['success' => false, 'data' => ['transfer_state' => -1], 'message' => '此平台没有查询订单功能'];
    }

    public function curlKick($request)
    {
        $accountUserName = $request->get('accountUserName');
        $url = $this->gameUrl . 'player/logout/playername/' . $accountUserName;
        $output = $this->ptcurl($url);
        $outputarr = json_decode($output, true);
        if (isset($outputarr['result']['result'])) {
            return ['success' => true, 'data' => [], 'message' => ''];
        } else {
            return ['success' => false, 'data' => [], 'message' => '踢线失败'];
        }
    }

    //进入游戏

    public function curlJoinGame($request)
    {
        $accountUserName = $request->input('accountUserName');
        $password = $request->input('password', '');
        $gamecode = $request->input('gamecode', '');
        $demo = $request->input('demo', '');
        if (empty($password)) {
            return ['success' => false, 'data' => [], 'message' => '密码为空'];
        }
        if (empty($gamecode)) {
            return ['success' => false, 'data' => [], 'message' => '游戏代码为空'];
        }
        if ($demo == '') {
            return ['success' => false, 'data' => [], 'message' => '试玩代码为空'];
        }
        return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password, 'gamecode' => $gamecode], 'message' => ''];
    }


    public function curlJoinMobileGame($request)
    {
        $accountUserName = $request->input('accountUserName');
        $password = $request->input('password', '');
        $gamecode = $request->input('gamecode', '');
        $demo = $request->input('demo', '');
        if (empty($password)) {
            return ['success' => false, 'data' => [], 'message' => '密码为空'];
        }
        if (empty($gamecode)) {
            return ['success' => false, 'data' => [], 'message' => '游戏代码为空'];
        }
        if ($demo == '') {
            return ['success' => false, 'data' => [], 'message' => '试玩代码为空'];
        }
        return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password, 'gamecode' => $gamecode], 'message' => ''];
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $totalPage = $page = 1;

        $times = $this->parseTimeInterval($startTime, $endTime);

        foreach ($times as $time) {
            $start_time = $time['start'];
            $end_time = $time['end'];
            do {
                $url = $this->gameUrl . 'customreport/getdata/reportname/PlayerGames/startdate/' . $start_time . '/enddate/' . $end_time . '/frozen/all/timeperiod/specify/page/' . $page . '/perPage/500';
                $output = $this->ptcurl($url);
                \Log::info('pt抓单的返回值是'.$output);
                $output = json_decode($output, true);

                if (array_key_exists('result', $output)) {
                    $result = $output['result'];
                    $data = array_merge($data, $result);
                    $totalPage = $output['pagination']['totalPages'];
                }
                $page++;
            } while ($page <= $totalPage);
        }
      \Log::info('pt抓单的返回值组装之后是',array('data'=>$data));
        return $data;
    }


}